package com.sxx.beautifulcity.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.DraweeView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ImageDecodeOptions;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.entity.HolidayData;
import com.sxx.beautifulcity.entity.Poi_List;
import com.sxx.beautifulcity.entity.greendao.HolidayDaoEntity;
import com.sxx.beautifulcity.listenerInterface.OnMyItemClickListener;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/21.
 */
public class HolidayAdapter extends RecyclerView.Adapter<HolidayAdapter.HolidayAdapterViewHolder>{

    private List<HolidayDaoEntity> list;
    private OnMyItemClickListener listener;


    public  void  setOnMyItemClickListener(OnMyItemClickListener l){
        listener = l;
    }


    public void addData(List<HolidayDaoEntity> datas){
        list = datas;
        notifyDataSetChanged();
    }

    public HolidayAdapter(Context context) {

    }

    /***
     *对组件进行初始化
     */
    class HolidayAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView holidayNameTv;
        private SimpleDraweeView holidayDraweeView;
        private RatingBar holidayRatingBar;
        private LinearLayout holidayLinearlayot;
        private int position;

        public HolidayAdapterViewHolder(View v) {
            super(v);
            holidayNameTv = (TextView) v.findViewById(R.id.holidayNameTv);
            holidayDraweeView = (SimpleDraweeView) v.findViewById(R.id.holidayDraweeView);
            holidayRatingBar = (RatingBar) v.findViewById(R.id.holidayRatingBar);
            holidayLinearlayot = (LinearLayout) v.findViewById(R.id.holidayLinearlayot);
            holidayLinearlayot.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (list != null && list.size() > 0) {
                HolidayDaoEntity data = list.get(position);
                if (listener != null) {
                    listener.onMyItemClick(data);
                }
            }
        }
    }


    /***
     * 加载布局
     * @param viewGroup
     * @param viewType
     * @return
     */
    @Override
    public HolidayAdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.activity_holiday_village_item , null);

        return new HolidayAdapterViewHolder(v);
    }

    /***
     * 绑定数据
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(HolidayAdapterViewHolder holder, int position) {
        if (list!= null && list.size() > 0) {
            HolidayDaoEntity data = list.get(position);
            holder.holidayNameTv.setText(data.getName());
            holder.holidayRatingBar.setNumStars(data.getStar());
            if (data.getImage() != null && data.getImage().length() > 0) {
            String path = data.getImage();
            frescoGetImage(path , holder.holidayDraweeView);

            }
        }
        holder.position = position;
    }

    public void frescoGetImage(String path , SimpleDraweeView holidayDraweeView){
        Uri uri = Uri.parse(path);
        holidayDraweeView.setImageURI(uri);
    }

    @Override
    public int getItemCount() {

        return list != null && list.size() > 0 ? list.size() : 0;
    }

}
